package com.zz.parkingsystem.service.impl;

import com.zz.parkingsystem.entity.*;
import com.zz.parkingsystem.mapper.*;
import com.zz.parkingsystem.service.ShowVOService;
import com.zz.parkingsystem.vo.ShowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ShowVOServiceImpl implements ShowVOService {
    @Autowired
    private ExceptionOrderMapper exceptionOrderMapper;
    @Autowired
    private AppealUnpaidMapper appealUnpaidMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private AppealPayMapper appealPayMapper;
    @Autowired
    private RefundMapper refundMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public void update(ShowVO showVO) {
        //该订单状态为申诉中
        String oiNo = showVO.getOiNo();
        OrderItem orderItemNew = orderItemMapper.selOrderItemByOiNo(oiNo);
        Order order = orderMapper.selOrderByOrderItemId(orderItemNew.getOiId());
        order.setOrderStatus(3);
        orderMapper.updateByPrimaryKeySelective(order);
        //未支付申诉
        if(showVO.getState()==2){
            //添加异常订单
            ExceptionOrder exceptionOrder = new ExceptionOrder();
            exceptionOrder.setOiNo(showVO.getOiNo());
            exceptionOrder.setEoStatus(0);
            exceptionOrderMapper.insertSelective(exceptionOrder);
            //添加未支付申诉
            AppealUnpaid appealUnpaid = new AppealUnpaid();
            OrderItem orderItem = orderItemMapper.selOrderItemByOiNo(showVO.getOiNo());
            appealUnpaid.setOiId(orderItem.getOiId());
            appealUnpaid.setAuContent(showVO.getContent());
            appealUnpaid.setAuImg(showVO.getImg());
            appealUnpaid.setAuTime(new Date());
            appealUnpaid.setAuStatus(0);
            appealUnpaidMapper.insertSelective(appealUnpaid);
        }
        //已支付申诉
        if(showVO.getState()==3){
            //支付申诉表
            AppealPay appealPay = new AppealPay();
            OrderItem orderItem = orderItemMapper.selOrderItemByOiNo(showVO.getOiNo());
            appealPay.setOiId(orderItem.getOiId());
            appealPay.setApContent(showVO.getContent());
            appealPay.setApImg(showVO.getImg());
            appealPay.setApTime(new Date());
            appealPay.setApStatus(0);
            appealPayMapper.insertSelective(appealPay);
            AppealPay appealPay1 = appealPayMapper.selByOiId(orderItem.getOiId());
            //退款表
            Refund refund = new Refund();
            refund.setRefAmount(orderItem.getOiAmount());
            refund.setRefStatus(0);
            refund.setRefTime(new Date());
            refund.setApId(appealPay1.getApId());
            refundMapper.insertSelective(refund);
        }
    }
}
